package servlet;

import bean.Account;
import bean.load;
import bean.doc;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;


@WebServlet(name = "Judge", value = "/Judge")
public class Judge extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        String ur = request.getParameter("ur");
        String pwd = request.getParameter("pwd");
        String pow = request.getParameter("pow");
        HttpSession session = request.getSession();
        session.setAttribute("ur",ur);
        session.setAttribute("pwd",pwd);
        session.setAttribute("pow",pow);
        load ac = new load();
        String pwd1=null,pow1="1",pow2="2",pow3="3";
        if(pow.equals(pow2)){//医生
            String SQL = "select*from doc";
            ArrayList<doc> al = ac.loaddoc(SQL);
            for (int i = 0; i < al.size(); i++) {
                if (ur.equals(al.get(i).getDoc_id())) {
                    pwd1 = al.get(i).getDoc_pwd();
                }
            }
        } else if (pow.equals(pow1)) {//普通用户
            String SQL = "select*from act";
            ArrayList<Account> al = ac.loadact(SQL);
            for (int i = 0; i < al.size(); i++) {
                if (ur.equals(al.get(i).getId())) {
                    pwd1 = al.get(i).getPwd();
                }
            }
        } else {//管理员
            String SQL = "select*from act";
            ArrayList<Account> al = ac.loadact(SQL);
            for (int i = 0; i < al.size(); i++) {
                if (ur.equals(al.get(i).getId())) {
                    pwd1 = al.get(i).getPwd();
                }
            }
        }
        if(pow.equals(pow1)&&ur!=null&&pwd.equals(pwd1)){
            System.out.println("普通用户登录");
            response.sendRedirect("/HospitalAppointmentReg_Sys_war_exploded/jsp/top.jsp");
        }else if (pow.equals(pow2)&&ur!=null&&pwd.equals(pwd1)) {
            System.out.println("医生登录");
            response.sendRedirect("/HospitalAppointmentReg_Sys_war_exploded/jsp/docter.jsp");
        } else if (pow.equals(pow3)&&ur!=null&&pwd.equals(pwd1)) {
            System.out.println("管理员登录");
            response.sendRedirect("/HospitalAppointmentReg_Sys_war_exploded/jsp/admin.jsp");
        } else if (ur==null||pwd==null) {
            System.out.println("输入不能为空");
            response.sendRedirect("/HospitalAppointmentReg_Sys_war_exploded/jsp/login.jsp");
        }else {
            System.out.println("用户和密码不匹配");
            response.sendRedirect("/HospitalAppointmentReg_Sys_war_exploded/jsp/login.jsp");
        }

    }

}
